/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package generator.relationships;

import java.util.ArrayList;
import java.util.List;
import net.db.generator.JavaBaseClassWriter;
import net.db.generator.field.models.BaseFieldModel;
import net.db.generator.field.models.BaseRelationshipsModel;
import net.db.generator.utils.ClassGeneratorUtil;

/**
 *
 * @author abosancic
 */
public class JavaRelationWriter extends JavaBaseClassWriter{
    
    protected BaseRelationshipsModel[] referencesFields;
    protected DataBaseRelationshipsModel[] referenceModel;

    public JavaRelationWriter() {
        super();
    }

    @Override
    protected void generateGettersAndSetters() {
        super.generateGettersAndSetters();
        generateReferencesGettersAndSetters();
    }
 
    protected void generateReferencesGettersAndSetters() {
        
        if (referencesFields != null) {
            for (BaseRelationshipsModel fieldModel : referencesFields) {
                String field = fieldModel.getFieldName();
                String type = fieldModel.getFieldType();
                String fielTypePrefix = "";
                String fielTypePostfix = "";
                DataBaseRelationshipsModel referenceModel2 = null;
                if(fieldModel.getRelationshipsType() == BaseRelationshipsModel.ONE_TO_MANY)
                {
                    field = field + "s";
                    fielTypePrefix = "java.util.List<";
                    fielTypePostfix = ">";
                }
                
                for (DataBaseRelationshipsModel referenceModel1 : referenceModel) {             // fix for one to many
                    if (referenceModel1.getForeignTable().equals(getClassName()) && fieldModel.getRelationshipsType() == BaseRelationshipsModel.MANY_TO_ONE) {
                        referenceModel2 = referenceModel1;
                    }
                }
                
                if (referenceModel2 != null) {
                    stringClass.append(spaces).append("public ").append(fielTypePrefix).append(ClassGeneratorUtil.humanize(type)).append(fielTypePostfix).append(" ").append(ClassGeneratorUtil.humanizeToGetter(field)).append("() {\n");
                    stringClass.append(spaces).append(spaces).append("this.").append(ClassGeneratorUtil.humanizeToField(field))
                            .append(" = new DAO").append(ClassGeneratorUtil.humanize(type)).append("().find(this.").append(referenceModel2.getForeignKey()).append(");\n");

                    stringClass.append(spaces).append(spaces).append("return ").append("this.").append(ClassGeneratorUtil.humanizeToField(field)).append(";\n");
                    stringClass.append(spaces).append("}\n\n");
                }
                stringClass.append(spaces).append("public void ").append(ClassGeneratorUtil.humanizeToSetter(field)).append("(").append(fielTypePrefix).append(ClassGeneratorUtil.humanize(type)).append(fielTypePostfix).append(" ").append(ClassGeneratorUtil.humanizeToField(field)).append("){\n");
                stringClass.append(spaces).append(spaces).append("this.").append(ClassGeneratorUtil.humanizeToField(field)).append(" = ").append(ClassGeneratorUtil.humanizeToField(field)).append(";\n");
                stringClass.append(spaces).append("}\n\n");
            }
        }
    }

    @Override
    protected void generateFields() {
        
        List<DataBaseRelationshipsModel> models = new ArrayList<DataBaseRelationshipsModel>();
        for (DataBaseRelationshipsModel referenceModel1 : referenceModel) {
            models.add(referenceModel1);
        }
        
        BaseFieldModel tmpFieldModel[] = fields;
        ArrayList<BaseFieldModel> tmp = new ArrayList<>();
        
        for (BaseFieldModel fieldModel : tmpFieldModel) {
            String field = fieldModel.getFieldName();
            if(!models.contains(new DataBaseRelationshipsModel(null, getClassName(), field, null))){
                tmp.add(fieldModel);
            }
        }
        
        //this.fields = tmp.toArray(new BaseFieldModel[tmp.size()]);
        
        super.generateFields();
        
        // many-to-one
        List<BaseRelationshipsModel> addModel = new ArrayList<>();
        
        for (DataBaseRelationshipsModel referenceModel : models) {
            for (BaseFieldModel fieldModel : tmpFieldModel) {
                String field = fieldModel.getFieldName();
                if(referenceModel.getPrimaryKey().equals(field) && referenceModel.getPrimaryTable().equals(getClassName()))
                {
                    stringClass.append(spaces).append("private ").append("java.util.List<")
                               .append(ClassGeneratorUtil.humanize(referenceModel.getForeignTable())).append("> ")
                               .append(ClassGeneratorUtil.humanizeToField(referenceModel.getForeignTable())).append("s;\n\n");
                    
                    BaseRelationshipsModel moodel = new BaseRelationshipsModel(referenceModel.getForeignTable(), referenceModel.getForeignTable(), BaseRelationshipsModel.ONE_TO_MANY);
                    addModel.add(moodel);
                }
            }
        }
        
        // one-to-many
        for (DataBaseRelationshipsModel referenceModel : models) {
            for (BaseFieldModel fieldModel : tmpFieldModel) {
                String field = fieldModel.getFieldName();
                if(referenceModel.getPrimaryKey().equals(field) && referenceModel.getForeignTable().equals(getClassName()))
                {
                    stringClass.append(spaces).append("private ")
                               .append(ClassGeneratorUtil.humanize(referenceModel.getPrimaryTable())).append(" ")
                               .append(ClassGeneratorUtil.humanizeToField(referenceModel.getPrimaryTable())).append(";\n\n");
                    BaseRelationshipsModel moodel = new BaseRelationshipsModel(referenceModel.getPrimaryTable(), referenceModel.getPrimaryTable(), BaseRelationshipsModel.MANY_TO_ONE);
                    addModel.add(moodel);
                }
            }
        }
        
        ArrayList<BaseRelationshipsModel> tmpref = new ArrayList<>();

        for (int i = 0; i < addModel.size(); i++) {
            tmpref.add(addModel.get(i));
        }
        
        referencesFields = tmpref.toArray(new BaseRelationshipsModel[tmpref.size()]);
        
    }

    // setters and getters
    public DataBaseRelationshipsModel[] getReferenceModel() {
        return referenceModel;
    }

    public void setReferenceModel(DataBaseRelationshipsModel[] referenceModel) {
        this.referenceModel = referenceModel;
    }

    public String[] getImportPackages() {
        return importPackages;
    }

    public void setImportPackages(String[] importPackages) {
        this.importPackages = importPackages;
    }
    
}
